package com.zzb.myshowme.presenter.impl;

import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.model.PhoneRegisterToMessageModel;
import com.zzb.myshowme.model.impl.PhoneRegisterToMessageModelImpl;
import com.zzb.myshowme.presenter.PhoneRegisterToMessagePresenter;
import com.zzb.myshowme.view.IView.PhoneRegisterToMessageView;

/**
 * Created by Administrator on 2016/10/25.
 */
public class PhoneRegisterToMessagePresenterImpl implements PhoneRegisterToMessagePresenter, PhoneRegisterToMessageModel.OnRegisterFinishedListener {

    private PhoneRegisterToMessageView mPhoneRegisterToMessageView;
    private PhoneRegisterToMessageModel mPhoneRegisterToMessageModel;

    UserInfo user;
    public PhoneRegisterToMessagePresenterImpl(PhoneRegisterToMessageView mPhoneRegisterToMessageView) {
        this.mPhoneRegisterToMessageView = mPhoneRegisterToMessageView;
        this.mPhoneRegisterToMessageModel = new PhoneRegisterToMessageModelImpl();
    }

    @Override
    public void register(String phone, String nickname, String sex, String password, String password_again){
        if (!password.equals(password_again)) {
            mPhoneRegisterToMessageView.showToastError("两次密码输入不一致");
            return;
        }
        if (password.length() < 6) {
            mPhoneRegisterToMessageView.showToastError("密码的长度为6~16之间");
            return;
        }
        if (nickname.length() > 10||nickname.length()<2) {
            mPhoneRegisterToMessageView.showToastError("名字的长度为3~10之间");
            return;
        }
        user = new UserInfo();
        user.setUser(phone);
        user.setSex(sex);
        user.setNickName(nickname);
//        user.setPassword(password);
        mPhoneRegisterToMessageView.showProgress();
        mPhoneRegisterToMessageModel.register(user, this);
    }

    @Override
    public void onSuccess() {
        if (mPhoneRegisterToMessageView != null) {
            if(user != null){
                mPhoneRegisterToMessageView.hideProgress();
                mPhoneRegisterToMessageView.navigateToHome(user);
            }
        }
    }

    @Override
    public void onFailure(String error) {
        if (mPhoneRegisterToMessageView != null) {
            mPhoneRegisterToMessageView.hideProgress();
            mPhoneRegisterToMessageView.showToastError(error);
        }
    }
}
